/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
// 递归
class Solution {
private:
    vector<int> rst;
public:
    vector<int> inorderTraversal(TreeNode* root) {
        if(root == nullptr){
            return rst;
        }

        inorderTraversal(root->left);
        rst.push_back(root->val);
        inorderTraversal(root->right);

        return rst;
    }
};

// 迭代-栈
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> rst;
        stack<TreeNode*> stack;

        // if(root == nullptr) return rst;

        TreeNode* temp = root;
        while(temp){
            stack.push(temp);
            temp = temp->left;
        }

        while(!stack.empty()) {
            TreeNode* inorder = stack.top();
            rst.push_back(inorder->val);
            stack.pop();

            if(inorder->right != nullptr){
                temp = inorder->right;
                while(temp){
                    stack.push(temp);
                    temp = temp->left;
                }
            }
        }

        return rst;
    }
};